﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using System.Reflection;

namespace Janus.Cores.DynamicProxy.CastleDynamic.Interception
{
    /// <summary>
    /// 功能：拦截器抽象基类
    /// 作者：luochengbang@163.com 
    /// 日期：2012/12/19 11:59:18 
    /// 备注：本代码版权归Janus所有，严禁外传 
    /// </summary>
    public abstract class HandlingInterceptorBase : IInterceptor
    {
        public HandlingInterceptorBase()
        {

        }
        /// <summary>
        /// 执行拦截动作
        /// </summary>
        /// <param name="invocation"></param>
        public abstract void DoIntercept(IInvocation invocation);
        /// <summary>
        /// 根据传入的MemberInfo创建可识别的接口名称
        /// </summary>
        /// <param name="mInfo"></param>
        /// <returns></returns>
        protected string CreateName(MemberInfo mInfo)
        {
            if (null == mInfo)
            {
                return string.Empty;
            }

            string name = string.Empty;
            string[] tmp = mInfo.ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            if (tmp.Length > 1)
            {
                name = mInfo.DeclaringType.FullName + "." + tmp[1];
            }
            else
            {
                name = mInfo.DeclaringType.FullName + "." + mInfo.Name + "()";
            }
            return name;
        }

        #region IInterceptor 成员

        public void Intercept(IInvocation invocation)
        {
            DoIntercept(invocation);
        }

        #endregion
    }
}
